<? include('markdown.php');

if (strpos($SERVER['REQUESTURI'], '/blog') === 0) { pregmatch('#^(/\d{4})((/\d{2})(/(.*).html)?)?#', substr($SERVER['REQUESTURI'], 5), $match); header("Location: http://clrhome.org$match[1]$match[3]/#" . strreplace('-', '', $match[5]), true, 301); die(); }

if (fileexists($match = '..' . $SERVER['REQUEST_URI'] . '.txt')) { header('HTTP/1.1 200 OK'); header('Status: 200 OK');

if (substr($_SERVER['REQUEST_URI'], -6) == 'README')
    die(Markdown(file_get_contents($match)));

header('Content-Type: text/plain');
die(file_get_contents($match));

}

if (pregmatch('#^/(products|resources|upcoming)/(.*)$#', $SERVER['REQUESTURI'], $match) and fileexists('../' . $match[2])) { header("Location: /$match[2]", true, 301); die(); }

$as = array('Software products' => 'products', 'Online resources' => 'resources', 'Upcoming projects' => 'upcoming', 'News' => ''); $matches = array('Software products' => array(), 'Online resources' => array(), 'Upcoming projects' => array(), 'News' => array(), 'Tutorials' => array()); $rss = new DOMDocument; $s = implode('', $sss = pregsplit('#\W+#', strtolower(urldecode($SERVER['REQUESTURI'])), null, PREGSPLITNOEMPTY));

if ($ss = substr(strreplace('|', 's?|', strreplace(array('|the|', '|a|', '|an|', '|some|', '|this|', '|that|', '|these|', '|those|', '|here|', '|there|', '|then|', '|to|', '|of|', '|in|', '|for|', '|on|', '|with|', '|at|', '|by|', '|from|', '|up|', '|about|', '|into|', '|over|', '|after|', '|beneath|', '|under|', '|above|', '|am|', '|are|', '|is|', '|was|', '|were|', '|will|', '|won|', '|do|', '|and|', '|or|', '|not|', '|other|', '|so|', '|how|', '|who|', '|what|', '|when|', '|where|', '|why|', '|than|'), '|', '|' . implode('|', $sss) . '|')), 3, -1)) { foreach ($as as $akey => $a) { $rss->load("../${a}posts.xml"); $items = $rss->getElementsByTagName('item');

    foreach ($items as $item) {
        $link = $item->getElementsByTagName('link')->item(0)->nodeValue;

        if (preg_match("#^(.*\\#)?$s$#", $item->getElementsByTagName('guid')->item(0)->nodeValue)) {
            header('Location: ' . $item->getElementsByTagName('link')->item(0)->nodeValue, true, 301);
            die();
        }

        $author = $item->getElementsByTagName('author')->item(0)->nodeValue;
        $match = preg_match_all("#\\b($ss)\\b#i", $item->getElementsByTagName('description')->item(0)->nodeValue, $match);

        if ($match >= count($sss))
            $matches[$akey]["                               <a href=\"$link\">
                                    <b><span>" . $item->getElementsByTagName('title')->item(0)->nodeValue . '</span> by ' . substr($author, strpos($author, ' ') + 2, -1) . "</b>
                                    <i>$link</i>
                                </a>
"] = $match;
    }

    arsort($matches[$akey]);
    $matches[$akey] = array_slice($matches[$akey], 0, 25);
}

$tuts = glob('../tutorials/*book.xml');

foreach ($tuts as $tut) {
    $rss->load($tut);
    $tutkey = substr($tut, 13, -8);
    $chapters = $rss->getElementsByTagName('chapter');

    if ($s == $tutkey) {
        header("Location: /tutorials/$tutkey/", true, 301);
        die();
    }

    foreach ($chapters as $i => $chapter) {
        $link = "/tutorials/$tutkey/";
        $sections = $chapter->getElementsByTagName('section');

        if ($i)
            $link .= "p$i/";

        foreach ($sections as $section) {
            $body = '';

            if ($s == str_replace('-', '', $id = $section->getAttributeNS('http://www.w3.org/XML/1998/namespace', 'id'))) {
                header("Location: $link#$id", true, 301);
                die();
            }

            $paras = $section->getElementsByTagName('para');

            foreach ($paras as $para)
                $body .= $para->nodeValue . ' ';

            $paras = $section->getElementsByTagName('subscript');

            foreach ($paras as $para)
                $body .= $para->nodeValue . ' ';

            $match = preg_match_all("#\\b($ss)\\b#i", $body, $match);

            if ($match >= count($sss))
                $matches["Tutorials"]["                             <a href=\"$link#$id\">
                                    <b><span>" . $section->getElementsByTagName('title')->item(0)->nodeValue . '</span> by ' . substr($author, strpos($author, ' ') + 2, -1) . "</b>
                                    <i>$link</i>
                                </a>
"] = $match;
        }
    }

    arsort($matches["Tutorials"]);
    $matches["Tutorials"] = array_slice($matches["Tutorials"], 0, 25);
}

}

header('HTTP/1.1 404 Not Found'); header('Status: 404 Not Found'); ?> Search results - ClrHome